import { gerUserinfoByUserId } from '@/api/user';

//用户数据示例
const users = [
  {
    uuid: '08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
    name: 'Mattie',
    password: '123',
    avatar: '/static/images/Avatar-1.png'
  },
  {
    uuid: '3bb179af-bcc5-4fe0-9dac-c05688484649',
    name: 'Wallace',
    password: '123',
    avatar: '/static/images/Avatar-2.png'
  },
  {
    uuid: 'fdee46b0-4b01-4590-bdba-6586d7617f95',
    name: 'Tracy',
    password: '123',
    avatar: '/static/images/Avatar-3.png'
  },
  {
    uuid: '33c3693b-dbb0-4bc9-99c6-fa77b9eb763f',
    name: 'Juanita',
    password: '123',
    avatar: '/static/images/Avatar-4.png'
  }
];

//群数据示例
const groups = [
  {
    uuid: 'group-a42b-47b2-bb1e-15e0f5f9a19a',
    name: '小程序交流群',
    avatar: '/static/images/wx.png',
    userList: [
      '08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
      '3bb179af-bcc5-4fe0-9dac-c05688484649',
      'fdee46b0-4b01-4590-bdba-6586d7617f95',
      '33c3693b-dbb0-4bc9-99c6-fa77b9eb763f'
    ]
  },
  {
    uuid: 'group-4b01-4590-bdba-6586d7617f95',
    name: 'UniApp交流群',
    avatar: '/static/images/uniapp.png',
    userList: [
      '08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a',
      'fdee46b0-4b01-4590-bdba-6586d7617f95',
      '33c3693b-dbb0-4bc9-99c6-fa77b9eb763f'
    ]
  },
  {
    uuid: 'group-dbb0-4bc9-99c6-fa77b9eb763f',
    name: 'GoEasy交流群',
    avatar: '/static/images/goeasy.jpeg',
    userList: ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', '3bb179af-bcc5-4fe0-9dac-c05688484649']
  }
];

class RestApi {
  findFriends(user: User) {
    const friendList = users.filter((v) => v.uuid !== user.uuid);
    return friendList;
  }

  findGroups(user: User) {
    const groupList = groups.filter((v) => v.userList.find((id) => id === user.uuid));
    return groupList;
  }

  findUser(username: string, password: string) {
    const user = users.find((user) => user.name === username && user.password === password);
    if (user) {
      return new User(user.uuid, user.name, user.avatar);
    }
    return user;
  }

  findGroupById(groupId: string) {
    const group = groups.find((group) => group.uuid === groupId);
    if (!group) {
      return new Group('', '', '');
    }
    return new Group(group.uuid, group.name, group.avatar);
  }

  async findUserById(userId: string) {
    const { data: user } = await gerUserinfoByUserId(Number(userId));
    if (!user) {
      return new User('', '', '');
    }
    return new User(String(user.id), user.nickName, user.avatarUrl);
  }

  findGroupMembers(groupId: string) {
    const members: Record<string, User> = {};
    const group = groups.find((v) => v.uuid === groupId);
    users.map((user) => {
      const groupUserUuid = (group || { userList: [] }).userList.find((uuid) => {
        return uuid === user.uuid;
      });
      if (groupUserUuid) {
        members[groupUserUuid] = new User(user.uuid, user.name, user.avatar);
      }
    });
    return members;
  }
}

class User {
  uuid: string;
  name: string;
  avatar: string;
  constructor(uuid: string, name: string, avatar: string) {
    this.uuid = uuid;
    this.name = name;
    this.avatar = avatar;
  }
}

class Group {
  uuid: string;
  name: string;
  avatar: string;
  constructor(uuid: string, name: string, avatar: string) {
    this.uuid = uuid;
    this.name = name;
    this.avatar = avatar;
  }
}

export default new RestApi();
